<?php
if(!defined('INEZCMS')){
	exit('Access Denied');
}

$data["status"]=0;


$voteid=intval($_POST["voteid"]);
$chklist=$_POST["chk"];
if(empty($chklist)){
     $data["msg"]="请选择投票项目!"; 
   echo json_encode($data);
   exit;   
    
}

if(empty($voteid)){
   $data["msg"]="您没有选择投票主题!"; 
   echo json_encode($data);
   exit; 
}
$todate=date("Y-m-d");
$voteinfo = $ezhand->select('vote_subject','*',"enabled=1 and fromdate<='".$todate."' and todate>='".$todate."' and  subjectid=".$voteid);
if(empty($voteinfo)){

   $data["msg"]="投票主题已经关闭!"; 
   echo json_encode($data);
   exit; 
        
}
if($voteinfo["allowguest"]<>1){
if(empty($userlogininfo)){
   $data["msg"]="请登录后，再发表评论!"; 
   echo json_encode($data);
   exit;    
}
}
$ip=get_client_ip();
if($voteinfo["myinterval"]==0){
    $vid=$ezhand->select('vote_data',"id","subjectid='".$voteid."' and ip='".$ip."'"); 
    if(!empty($vid)){
       $data["msg"]="您已经投过票了，请不要重复投票!"; 
       echo json_encode($data);
       exit; 
           
    }
}else{
    $lasttime=time()-24*3600*$voteinfo["myinterval"];
    
    $vid=$ezhand->select('vote_data',"id","subjectid='".$voteid."' and 	votetime>='".$lasttime."'"); 
    if(!empty($vid)){
       $data["msg"]="您已经投过票了，请不要重复投票!"; 
       echo json_encode($data);
       exit; 
           
    }
    
}


while(list($k,$optionid)=each($chklist)){
    
    $vdata=array();
    $vdata["ip"]=get_client_ip();
    $vdata["subjectid"]=$voteid;
    $vdata["votetime"]=time();
    if(empty($userlogininfo)){
    $vdata["userid"]=0;
    $vdata["username"]="游客";
    }else{
    $vdata["userid"]=$userlogininfo["uid"];
    $vdata["username"]=$userlogininfo["ezusername"];
        
    }
    $vdata["optionid"]=$optionid;
        
    $qid = $ezhand->insert('vote_data', $vdata); 

}

$data2=array('f'=>'votenumber','e'=>'+','v'=>1);
$ezhand->update('vote_subject',$data2,'subjectid="'.$voteid.'"');  
 


$data["msg"]="投票成功，谢谢!"; 
echo json_encode($data);


?>